
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  pool.bean;

import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import control.RequestParameter;

import dbmanager.DBManager;

import pool.tablename.Transaction;


import utility.ConvertToLower;


public class Transaction_Bean extends SubBean
{

	private String tableName = "transaction";
	private String pl_tableName = "trans_property_details";
	private String td_tableName = "transaction_details";
	//private String process = "";
	
	private Hashtable<Object,Object> TransactionHash;
	private Hashtable<Object,Object> oldTransactionHash;
	private Hashtable<Object,Object> newTransactionHash;
	
	private Transaction objTranasaction;
	
	//vector containing properties to be inserted in trans_property_details
	private Vector<Object> TransPropertyVector = new Vector<Object>();
	
	//vector containing transaction_details object & its properties 
	//to be inserted in transaction_details & td_property_details respectively
	private Vector<Object> TransDetailVector = new Vector<Object>() ;
	
	private Hashtable<Object,Object> ViewHashtable = new Hashtable<Object, Object>();
	private Hashtable<Object,Object> PropertyViewHashtable = new Hashtable<Object, Object>();
	private Hashtable<Object,Object> TransDetailsViewHashtable = new Hashtable<Object, Object>();
	PoolLogger pl;
	
	public Transaction_Bean() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	/**
	 * 
	 * @param PropertyHash is Hashtable of transaction for insertion into transaction.   
	 */
	public void setHashtable(Hashtable<Object,Object> TransHash)
	{	
		objTranasaction=new Transaction();
		
		TransactionHash = new Hashtable<Object, Object>();
		TransactionHash.putAll(TransHash);
		TransactionHash=ConvertToLower.convertHashKey(TransactionHash);
	}

		
	/**
	 * this function converts properties in vector in Trans__property_details class format to insert
	 * @param vector containing properties
	 */
	@SuppressWarnings("unchecked")
	public void setPropertyVector(Vector<Object> vector)
	{
		
		Vector<Object> tempVector = new Vector<Object>();		
		tempVector=vector;
			
		Trans_property_details_Bean tplBean = new Trans_property_details_Bean();
		tplBean.initPool();
		tplBean.setReqParam(reqParam);
		for(Object object : tempVector)
		{
			Hashtable<Object,Object> hashtable = new Hashtable<Object, Object>();
			hashtable.putAll((Hashtable<Object, Object>)object);
			tplBean.setPropertyVector(hashtable);		
		}
		
		TransPropertyVector=tplBean.getPropertyVector();		
		
	}
	
	
	/**
	 * This function sets vector of Transaction_details class
	 */
	public void setTransDetailsVector(Vector<Object> vector)
	{
		TransDetailVector.addAll(vector);	
	}
	
	
	/**
	 * Insert into Transaction table. 
	 *  
	 */	
	public StringBuffer insert()
	{
		StringBuffer sbrDTB = new StringBuffer();
		Vector<Object> TdPropertyVector = new Vector<Object>();
		
		DBManager.setBeforeCommit();
		
		objTranasaction = new Transaction();
		
		objTranasaction.initPool();
		objTranasaction.setRequestParameter(reqParam);
		objTranasaction.setHashtable(TransactionHash);		
		
		Transaction_details_Bean tdBean=new Transaction_details_Bean();
		tdBean.initPool();
		tdBean.setReqParam(reqParam);
		tdBean.setPropertyVector(TransDetailVector);
		
		TdPropertyVector=tdBean.getPropertyVector();
		
		objTranasaction.setTransDetailsVector(TdPropertyVector);		
		
		//objTranasaction.tpd = new Trans_property_details();
		objTranasaction.tpd.initPool();		
		objTranasaction.tpd.setRequestParameter(reqParam);
		if(!TransPropertyVector.isEmpty())
			objTranasaction.tpd.setPropertyVector(TransPropertyVector);
		
		sbrDTB.append(objTranasaction.getInsert());
		
		if(sbrDTB.toString().toLowerCase().indexOf("error")==-1)
		{
			objTranasaction.td.setRequestParameter(reqParam);
			objTranasaction.td.initPool();			
			objTranasaction.td.setTid(objTranasaction.getId());
			StringBuffer strbuffer =new StringBuffer();
			//strbuffer.append(objTranasaction.td.getInsert());
			if(strbuffer.toString().contains("ErrorInput"))
				return strbuffer;
			else				
				sbrDTB.append(objTranasaction.td.getInsert());
		}	
		boolean flag = objTranasaction.getResult();
		DBManager.setAfterCommit(flag);
		
		System.out.println(sbrDTB.toString());
		pl.info(sbrDTB.toString());
		pl.debug(sbrDTB.toString());
		pl.warn(sbrDTB.toString());
		pl.fatal(sbrDTB.toString());
		pl.error(sbrDTB.toString());
		pl.trace(sbrDTB.toString());
		
		return sbrDTB;
	}
		
	
	/**
	 * 
	 * @param oldHash  is Hashtable of old property of transaction .  
	 */
	public void setOldHashtable(Hashtable<Object, Object> oldHash) 
	{
		objTranasaction=new Transaction();
		objTranasaction.setRequestParameter(reqParam);
		oldTransactionHash = new Hashtable<Object, Object>();
		oldTransactionHash.putAll(oldHash);
		oldTransactionHash=ConvertToLower.convertHashKey(oldTransactionHash);
		oldTransactionHash = gin.convertMasterId(oldTransactionHash, tableName,process);
	}
	
	
	/**
	 * @param newPropertyHash  is a  Hashtable of new property for updation of transaction.   
	 */
	public void setNewHashtable(Hashtable<Object,Object> newPropertyHash)
	{
		objTranasaction=new Transaction();
		objTranasaction.setRequestParameter(reqParam);
		newTransactionHash = new Hashtable<Object, Object>();
		newTransactionHash.putAll(newPropertyHash);
		newTransactionHash=ConvertToLower.convertHashKey(newTransactionHash);
	}
	
	
	/**
	 * update transaction table.
	 *
	 */
	public StringBuffer update()
	{		 
		StringBuffer sbrDTB = new StringBuffer();
		
		DBManager.setBeforeCommit();		
		objTranasaction = new Transaction();
		objTranasaction.setRequestParameter(reqParam);
		objTranasaction.setOldProperty(oldTransactionHash);
		objTranasaction.setNewProperty(newTransactionHash);
		sbrDTB.append(objTranasaction.getUpdate());
		if(sbrDTB.toString().toLowerCase().indexOf("error")==-1)
		{
			objTranasaction.td.setRequestParameter(reqParam);
			objTranasaction.td.initPool();			
			objTranasaction.td.setTid(objTranasaction.getId());
			sbrDTB.append(objTranasaction.td.getUpdate());
		}
		boolean flag = objTranasaction.getResult();
		DBManager.setAfterCommit(flag);
		
		System.out.println(sbrDTB.toString());
		pl.info(sbrDTB.toString());
		pl.debug(sbrDTB.toString());
		pl.warn(sbrDTB.toString());
		pl.fatal(sbrDTB.toString());
		pl.error(sbrDTB.toString());
		pl.trace(sbrDTB.toString());		
		return sbrDTB;	
	}
		

	/**
	 * 
	 * @param hashtable  is Hashtable of transaction to view .  
	 */
	public void setViewHashtable(Hashtable<Object,Object> hashtable)
	{

		Hashtable<Object,Object> PropertyHash = new Hashtable<Object, Object>();
		
		PropertyHash.putAll(hashtable);		
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));
		PropertyHash.putAll(ConvertToLower.convertHashKey(PropertyHash));
		PropertyHash = gin.convertMasterId(PropertyHash,tableName,process);		
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));	
		
		ViewHashtable.putAll(PropertyHash);
	}
	
	/**
	 * 
	 * @param hashtable  is Hashtable of trans_property_details to view . 
	 */
	public void setPropertyViewHashtable(Hashtable<Object,Object> hashtable)
	{

		Hashtable<Object,Object> PropertyHash = new Hashtable<Object, Object>();
		
		PropertyHash.putAll(hashtable);		
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));
		PropertyHash.putAll(ConvertToLower.convertHashKey(PropertyHash));
		PropertyHash = gin.convertMasterId(PropertyHash, pl_tableName,process);		
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));	
		
		 PropertyViewHashtable.putAll(PropertyHash);	
	}
	
	
	/**
	 * 
	 * @param hashtable  is Hashtable of transaction_details to view . 
	 */
	public void setTransDetailsViewHashtable(Hashtable<Object,Object> hashtable)
	{

		Hashtable<Object,Object> PropertyHash = new Hashtable<Object, Object>();
		
		PropertyHash.putAll(hashtable);		
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));
		PropertyHash.putAll(ConvertToLower.convertHashKey(PropertyHash));
		PropertyHash = gin.convertMasterId(PropertyHash, td_tableName,process);		
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));	
		
		 TransDetailsViewHashtable.putAll(PropertyHash);	
	}
	
	
	/**
	 * view() calls getView() of Transaction class
	 * @return stringbuffer containing details of transaction in message format
	 */	
	public StringBuffer view()
	{
		StringBuffer sbrDTB = new StringBuffer();
		objTranasaction = new Transaction();
		objTranasaction.setRequestParameter(reqParam);
		objTranasaction.setProcess(process);
		
		objTranasaction.setViewHashtable(ViewHashtable);		
		objTranasaction.setPropertyViewHashtable(PropertyViewHashtable);
		objTranasaction.setTransDetailsViewHashtable(TransDetailsViewHashtable);	
		
		sbrDTB.append(objTranasaction.getView());
		System.out.println(sbrDTB.toString());
		pl.info(sbrDTB.toString());
		pl.debug(sbrDTB.toString());
		pl.warn(sbrDTB.toString());
		pl.fatal(sbrDTB.toString());
		pl.error(sbrDTB.toString());
		pl.trace(sbrDTB.toString());
	
		return sbrDTB;
	
	}

	@Override
	public StringBuffer delete() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
	/*public void setOldTransHashtable(Hashtable<Object,Object> oldHash)
	{
		oldTransactionHash.putAll(oldHash);
	}
	public void setNewTransHashtable(Hashtable<Object,Object> newHash)
	{
		newTransactionHash.putAll(newHash);
	}*/
	/**
	 * 
	 * @param TransDetailHashMap is a hashtable of values of Transacion_details.     
	 */
	
	/*public void setTransDetailHash(Hashtable<Object,Object> PropertyHash)
	{
		ExecutionTimer t = new ExecutionTimer();
		 t.start();
		PropertyHash = gin.convertMasterId(PropertyHash, tableName,process);
		this.TransDetailHashMap=PropertyHash;
		t.end();
		TimerRecordFile timerFile=new TimerRecordFile("pool.bean","Transaction_Bean","setTransDetailHash",t.duration());
		
	}*/
	
	
	
	
	public static void main(String[] args) 
	{
		Transaction_Bean prop = new Transaction_Bean();
		
		prop.initPool();
		Hashtable<Object, Object> old = new Hashtable<Object, Object>();
		/*old.put("Transaction_type", "PO");
		old.put("Buyer", "aab");*/
		old.put("seller", "acd");
	
		Hashtable<Object, Object> oldHash = new Hashtable<Object, Object>();
		oldHash.put("id", "1");
		/*oldHash.put("Transaction_type", "PO");
		oldHash.put("Buyer","aab");
		oldHash.put("seller","acd");*/
		
		
		Hashtable<Object, Object> newHash = new Hashtable<Object, Object>();
		newHash.put("id", "115");
		newHash.put("Transaction_type", "PO");
		newHash.put("Buyer", "aaa");
		newHash.put("seller", "adc");
		
		Hashtable<Object, Object> PropertyHash = new Hashtable<Object, Object>();
		PropertyHash.put("item","aaa");
		PropertyHash.put("qty", "18814");
		
		

		RequestParameter req = new RequestParameter();
		req.setUsername("+919922930640");
		req.setMessage("System Message");
		req.setPassword("123");
		req.setRequest("transaction");
		req.setProcess("add");
		
	
		prop.setReqParam(req);
		//prop.setReqParam(req);
		
		
	/*	prop.setTransactionash(old);
		prop.setOldTranasactionHashUpdate(oldHash);
		prop.setNewTransactionHash(newHash);
		prop.setTransDetailHash(PropertyHash);
		
		prop.insertTransaction();*/
		//prop.updateTransaction();
	
	}

	
}
